import {_decorator, Component, Label} from 'cc';
import EventCenter from '../event/EventCenter';
import {GameEvent} from '../event/GameEvent';

const { ccclass, property } = _decorator;

@ccclass('progress')
export class progress extends Component {
    @property(Label)
    label: Label = null;

    progress: number = 0;
    goal: number = 0;

    setGoal(goal: number) {
        this.goal = goal;
        this.updateProgress();
    }

    addProgress(progress: number = 1) {
        this.progress += progress;
        if (this.progress > this.goal) {
            this.progress = this.goal;
        }
        this.updateProgress();
    }

    setProgress(progress: number) {
        this.progress = progress;
        if (this.progress > this.goal) {
            this.progress = this.goal;
        }
        this.updateProgress();
    }

    updateProgress() {
        this.label.string = '已完成订单:' + this.progress + '/' + this.goal;
    }

    protected onLoad() {
        EventCenter.on(GameEvent.ProgressAdded, this.addProgress, this);
    }

    protected onDestroy() {
        EventCenter.off(GameEvent.ProgressAdded, this.addProgress, this);
    }
}


